﻿using Core;
using RimWorld;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;
using Verse;
using Verse.Noise;
using Verse.Sound;
using static RimWorld.PsychicRitualRoleDef;

namespace RI_Talisman
{
    public class CompAbilityEffect_RI_TurretAssemble : CompAbilityEffect
    {
        public new CompProperties_RI_TurretAssemble Props => (CompProperties_RI_TurretAssemble)props;

        private static Vector3 MoteCastOffset = new Vector3(0f, 0f, 0);

        public override void Initialize(AbilityCompProperties props)
        {
            base.Initialize(props);
        }

        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
        {

            List<Thing> things = new List<Thing>();
            List<IntVec3> cells = AffectedCells(target, parent.pawn.Map, Props.radius).ToList<IntVec3>();
            foreach (IntVec3 cell in cells)
            {
                foreach (Thing thing in cell.GetThingList(parent.pawn.Map))
                {
                    things.Add(thing);
                }
            }
            foreach (Thing item in things)
            {
                if (item.def.butcherProducts?.First().thingDef.defName.Substring(0,5) == "Block")
                {
                    IntVec3 position = item.Position;
                    item.Destroy();
                    if (position.GetFirstBuilding(parent.pawn.Map) == null) {
                        Thing turret= ThingMaker.MakeThing(Props.turretDefStone ,item.def.butcherProducts?.First().thingDef);
                        GenSpawn.Spawn(turret, position, parent.pawn.Map);
                        turret.SetFaction(parent.pawn.Faction);
                        //特效
                        ThingDef moteCast = Props.moteCastDef;
                        MoteMaker.MakeAttachedOverlay(turret, moteCast, MoteCastOffset, 1);
                    }
                }
                else if (item.def.defName == "ChunkSlagSteel")
                {
                    IntVec3 position = item.Position;
                    item.Destroy();
                    if (position.GetFirstBuilding(parent.pawn.Map) == null)
                    {
                        Thing turret = ThingMaker.MakeThing(Props.turretDefSteel, item.def.butcherProducts?.First().thingDef);
                        GenSpawn.Spawn(turret, position, parent.pawn.Map);
                        turret.SetFaction(parent.pawn.Faction);
                    }

                }
            }
        }
    
        public override void CompTick()
        {
          
            base.CompTick();

        }
        private IEnumerable<IntVec3> AffectedCells(LocalTargetInfo target, Map map, int radius)
        {
            foreach (IntVec3 item in GenRadial.RadialCellsAround(target.Cell, radius, useCenter: true))
            {
                if (item.InBounds(map))
                {
                    yield return item;
                }
            }
        }
    }
}